package com.hytc.mymall.utils;

import android.net.ParseException;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/1.
 */

public class IdcardUtils {
    public static final int CHINA_ID_MAX_LENGTH = 18;
    public static final int CHINA_ID_MIN_LENGTH = 15;
    public static final int MIN = 1930;
    public static final String[] cityCode;
    public static Map<String, String> cityCodes;
    public static Map<String, Integer> hkFirstCode;
    public static final int[] power;
    public static Map<String, Integer> twFirstCode;
    public static final String[] verifyCode;

    static {
        cityCode = new String[] { "11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82", "91" };
        power = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        verifyCode = new String[] { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
        IdcardUtils.cityCodes = new HashMap<String, String>();
        IdcardUtils.twFirstCode = new HashMap<String, Integer>();
        IdcardUtils.hkFirstCode = new HashMap<String, Integer>();
        IdcardUtils.cityCodes.put("11", "\u5317\u4eac");
        IdcardUtils.cityCodes.put("12", "\u5929\u6d25");
        IdcardUtils.cityCodes.put("13", "\u6cb3\u5317");
        IdcardUtils.cityCodes.put("14", "\u5c71\u897f");
        IdcardUtils.cityCodes.put("15", "\u5185\u8499\u53e4");
        IdcardUtils.cityCodes.put("21", "\u8fbd\u5b81");
        IdcardUtils.cityCodes.put("22", "\u5409\u6797");
        IdcardUtils.cityCodes.put("23", "\u9ed1\u9f99\u6c5f");
        IdcardUtils.cityCodes.put("31", "\u4e0a\u6d77");
        IdcardUtils.cityCodes.put("32", "\u6c5f\u82cf");
        IdcardUtils.cityCodes.put("33", "\u6d59\u6c5f");
        IdcardUtils.cityCodes.put("34", "\u5b89\u5fbd");
        IdcardUtils.cityCodes.put("35", "\u798f\u5efa");
        IdcardUtils.cityCodes.put("36", "\u6c5f\u897f");
        IdcardUtils.cityCodes.put("37", "\u5c71\u4e1c");
        IdcardUtils.cityCodes.put("41", "\u6cb3\u5357");
        IdcardUtils.cityCodes.put("42", "\u6e56\u5317");
        IdcardUtils.cityCodes.put("43", "\u6e56\u5357");
        IdcardUtils.cityCodes.put("44", "\u5e7f\u4e1c");
        IdcardUtils.cityCodes.put("45", "\u5e7f\u897f");
        IdcardUtils.cityCodes.put("46", "\u6d77\u5357");
        IdcardUtils.cityCodes.put("50", "\u91cd\u5e86");
        IdcardUtils.cityCodes.put("51", "\u56db\u5ddd");
        IdcardUtils.cityCodes.put("52", "\u8d35\u5dde");
        IdcardUtils.cityCodes.put("53", "\u4e91\u5357");
        IdcardUtils.cityCodes.put("54", "\u897f\u85cf");
        IdcardUtils.cityCodes.put("61", "\u9655\u897f");
        IdcardUtils.cityCodes.put("62", "\u7518\u8083");
        IdcardUtils.cityCodes.put("63", "\u9752\u6d77");
        IdcardUtils.cityCodes.put("64", "\u5b81\u590f");
        IdcardUtils.cityCodes.put("65", "\u65b0\u7586");
        IdcardUtils.cityCodes.put("71", "\u53f0\u6e7e");
        IdcardUtils.cityCodes.put("81", "\u9999\u6e2f");
        IdcardUtils.cityCodes.put("82", "\u6fb3\u95e8");
        IdcardUtils.cityCodes.put("91", "\u56fd\u5916");
        IdcardUtils.twFirstCode.put("A", 10);
        IdcardUtils.twFirstCode.put("B", 11);
        IdcardUtils.twFirstCode.put("C", 12);
        IdcardUtils.twFirstCode.put("D", 13);
        IdcardUtils.twFirstCode.put("E", 14);
        IdcardUtils.twFirstCode.put("F", 15);
        IdcardUtils.twFirstCode.put("G", 16);
        IdcardUtils.twFirstCode.put("H", 17);
        IdcardUtils.twFirstCode.put("J", 18);
        IdcardUtils.twFirstCode.put("K", 19);
        IdcardUtils.twFirstCode.put("L", 20);
        IdcardUtils.twFirstCode.put("M", 21);
        IdcardUtils.twFirstCode.put("N", 22);
        IdcardUtils.twFirstCode.put("P", 23);
        IdcardUtils.twFirstCode.put("Q", 24);
        IdcardUtils.twFirstCode.put("R", 25);
        IdcardUtils.twFirstCode.put("S", 26);
        IdcardUtils.twFirstCode.put("T", 27);
        IdcardUtils.twFirstCode.put("U", 28);
        IdcardUtils.twFirstCode.put("V", 29);
        IdcardUtils.twFirstCode.put("X", 30);
        IdcardUtils.twFirstCode.put("Y", 31);
        IdcardUtils.twFirstCode.put("W", 32);
        IdcardUtils.twFirstCode.put("Z", 33);
        IdcardUtils.twFirstCode.put("I", 34);
        IdcardUtils.twFirstCode.put("O", 35);
        IdcardUtils.hkFirstCode.put("A", 1);
        IdcardUtils.hkFirstCode.put("B", 2);
        IdcardUtils.hkFirstCode.put("C", 3);
        IdcardUtils.hkFirstCode.put("R", 18);
        IdcardUtils.hkFirstCode.put("U", 21);
        IdcardUtils.hkFirstCode.put("Z", 26);
        IdcardUtils.hkFirstCode.put("X", 24);
        IdcardUtils.hkFirstCode.put("W", 23);
        IdcardUtils.hkFirstCode.put("O", 15);
        IdcardUtils.hkFirstCode.put("N", 14);
    }

    public static String conver15CardTo18(final String s) {
        if (s.length() == 15 && isNum(s)) {
            final String substring = s.substring(6, 12);
            while (true) {
                try {
                    final Date parse = new SimpleDateFormat("yyMMdd").parse(substring);
                    final Calendar instance = Calendar.getInstance();
                    if (parse != null) {
                        instance.setTime(parse);
                    }
                    //不确定Calendar.MONTH
                    String s2 = String.valueOf(s.substring(0, 6)) + String.valueOf(instance.get(Calendar.YEAR)) + s.substring(8);
                    final char[] charArray = s2.toCharArray();
                    if (charArray != null) {
                        final String checkCode18 = getCheckCode18(getPowerSum(converCharToInt(charArray)));
                        if (checkCode18.length() <= 0) {
                            return null;
                        }
                        s2 = String.valueOf(s2) + checkCode18;
                    }
                    return s2;
                }
                catch (ParseException ex) {
                    ex.printStackTrace();
                    final Date parse = null;
                    continue;
                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
        return null;
    }

    public static int[] converCharToInt(final char[] array) {
        final int length = array.length;
        final int[] array2 = new int[length];
        int i = 0;
        while (i < length) {
            try {
                array2[i] = Integer.parseInt(String.valueOf(array[i]));
                ++i;
            }
            catch (NumberFormatException ex) {
                ex.printStackTrace();
                return array2;
            }
        }
        return array2;
    }

    public static int getAgeByIdCard(String conver15CardTo18) {
        if (conver15CardTo18.length() == 15) {
            conver15CardTo18 = conver15CardTo18(conver15CardTo18);
        }
        return Calendar.getInstance().get(Calendar.YEAR) - Integer.valueOf(conver15CardTo18.substring(6, 10));
    }

    public static String getBirthByIdCard(String conver15CardTo18) {
        final Integer value = conver15CardTo18.length();
        if (value < 15) {
            return null;
        }
        if (value == 15) {
            conver15CardTo18 = conver15CardTo18(conver15CardTo18);
        }
        return conver15CardTo18.substring(6, 14);
    }

    public static String getCheckCode18(final int n) {
        switch (n % 11) {
            default: {
                return "";
            }
            case 10: {
                return "2";
            }
            case 9: {
                return "3";
            }
            case 8: {
                return "4";
            }
            case 7: {
                return "5";
            }
            case 6: {
                return "6";
            }
            case 5: {
                return "7";
            }
            case 4: {
                return "8";
            }
            case 3: {
                return "9";
            }
            case 2: {
                return "x";
            }
            case 1: {
                return "0";
            }
            case 0: {
                return "1";
            }
        }
    }

    public static Short getDateByIdCard(String conver15CardTo18) {
        final Integer value = conver15CardTo18.length();
        if (value < 15) {
            return null;
        }
        if (value == 15) {
            conver15CardTo18 = conver15CardTo18(conver15CardTo18);
        }
        return Short.valueOf(conver15CardTo18.substring(12, 14));
    }

    public static String getGenderByIdCard(String conver15CardTo18) {
        if (conver15CardTo18.length() == 15) {
            conver15CardTo18 = conver15CardTo18(conver15CardTo18);
        }
        if (Integer.parseInt(conver15CardTo18.substring(16, 17)) % 2 != 0) {
            return "M";
        }
        return "F";
    }

    public static Short getMonthByIdCard(String conver15CardTo18) {
        final Integer value = conver15CardTo18.length();
        if (value < 15) {
            return null;
        }
        if (value == 15) {
            conver15CardTo18 = conver15CardTo18(conver15CardTo18);
        }
        return Short.valueOf(conver15CardTo18.substring(10, 12));
    }

    public static int getPowerSum(final int[] array) {
        final int length = IdcardUtils.power.length;
        final int length2 = array.length;
        int n = 0;
        if (length == length2) {
            for (int i = 0; i < array.length; ++i) {
                for (int j = 0; j < IdcardUtils.power.length; ++j) {
                    if (i == j) {
                        n += array[i] * IdcardUtils.power[j];
                    }
                }
            }
        }
        return n;
    }

    public static String getProvinceByIdCard(final String s) {
        final int length = s.length();
        String substring = "";
        if (length == 15 || length == 18) {
            substring = s.substring(0, 2);
        }
        return IdcardUtils.cityCodes.get(substring);
    }

    public static Short getYearByIdCard(String conver15CardTo18) {
        final Integer value = conver15CardTo18.length();
        if (value < 15) {
            return null;
        }
        if (value == 15) {
            conver15CardTo18 = conver15CardTo18(conver15CardTo18);
        }
        return Short.valueOf(conver15CardTo18.substring(6, 10));
    }

    public static boolean isNum(final String s) {
        return s != null && !"".equals(s) && s.matches("^[0-9]*$");
    }

    public static boolean valiDate(final int n, final int n2, final int n3) {
        int n4 = 1;
        final int value = Calendar.getInstance().get(Calendar.YEAR);
        if (n < 1930 || n >= value) {
            n4 = 0;
        }
        else {
            if (n2 < n4 || n2 > 12) {
                return false;
            }
            int n5 = 0;
            switch (n2) {
                default: {
                    n5 = 31;
                    break;
                }
                case 4:
                case 6:
                case 9:
                case 11: {
                    n5 = 30;
                    break;
                }
                case 2: {
                    int n6;
                    if (((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) && n > 1930 && n < value) {
                        n6 = n4;
                    }
                    else {
                        n6 = 0;
                    }
                    if (n6 != 0) {
                        n5 = 29;
                    }
                    else {
                        n5 = 28;
                    }
                    break;
                }
            }
            if (n3 < n4 || n3 > n5) {
                return false;
            }
        }
        return n4 != 0;
    }

    public static boolean validateCard(final String s) {
        final String trim = s.trim();
        if (!validateIdCard18(trim) && !validateIdCard15(trim)) {
            final String[] validateIdCard10 = validateIdCard10(trim);
            if (validateIdCard10 == null || !validateIdCard10[2].equals("true")) {
                return false;
            }
        }
        return true;
    }

    public static boolean validateHKCard(final String s) {
        String s2 = s.replaceAll("[\\(|\\)]", "");
        Integer n;
        if (s2.length() == 9) {
            n = 9 * (-55 + Integer.valueOf(s2.substring(0, 1).toUpperCase().toCharArray()[0])) + 8 * (-55 + Integer.valueOf(s2.substring(1, 2).toUpperCase().toCharArray()[0]));
            s2 = s2.substring(1, 9);
        }
        else {
            n = 522 + 8 * (-55 + Integer.valueOf(s2.substring(0, 1).toUpperCase().toCharArray()[0]));
        }
        final String substring = s2.substring(1, 7);
        final String substring2 = s2.substring(7, 8);
        final char[] charArray = substring.toCharArray();
        Integer n2 = 7;
        for (int length = charArray.length, i = 0; i < length; ++i) {
            n += Integer.valueOf(new StringBuilder(String.valueOf(charArray[i])).toString()) * n2;
            --n2;
        }
        Integer n3;
        if (substring2.toUpperCase().equals("A")) {
            n3 = 10 + n;
        }
        else {
            n3 = n + Integer.valueOf(substring2);
        }
        return n3 % 11 == 0;
    }

    public static String[] validateIdCard10(final String s) {
        final String[] array = new String[3];
        final String replaceAll = s.replaceAll("[\\(|\\)]", "");
        if (replaceAll.length() != 8 && replaceAll.length() != 9 && s.length() != 10) {
            return null;
        }
        if (s.matches("^[a-zA-Z][0-9]{9}$")) {
            array[0] = "\u53f0\u6e7e";
            System.out.println("11111");
            final String substring = s.substring(1, 2);
            if (substring.equals("1")) {
                array[1] = "M";
                System.out.println("MMMMMMM");
            }
            else {
                if (!substring.equals("2")) {
                    array[1] = "N";
                    array[2] = "false";
                    System.out.println("NNNN");
                    return array;
                }
                array[1] = "F";
                System.out.println("FFFFFFF");
            }
            String s2;
            if (validateTWCard(s)) {
                s2 = "true";
            }
            else {
                s2 = "false";
            }
            array[2] = s2;
            return array;
        }
        if (s.matches("^[1|5|7][0-9]{6}\\(?[0-9A-Z]\\)?$")) {
            array[0] = "\u6fb3\u95e8";
            array[1] = "N";
            return array;
        }
        if (s.matches("^[A-Z]{1,2}[0-9]{6}\\(?[0-9A]\\)?$")) {
            array[0] = "\u9999\u6e2f";
            array[1] = "N";
            String s3;
            if (validateHKCard(s)) {
                s3 = "true";
            }
            else {
                s3 = "false";
            }
            array[2] = s3;
            return array;
        }
        return null;
    }

    public static boolean validateIdCard15(final String s) {
        if (s.length() == 15 && isNum(s) && IdcardUtils.cityCodes.get(s.substring(0, 2)) != null) {
            final String substring = s.substring(6, 12);
            while (true) {
                try {
                    final Date parse = new SimpleDateFormat("yy").parse(substring.substring(0, 2));
                    final Calendar instance = Calendar.getInstance();
                    if (parse != null) {
                        instance.setTime(parse);
                    }
                    if (valiDate(instance.get(Calendar.YEAR), Integer.valueOf(substring.substring(2, 4)), Integer.valueOf(substring.substring(4, 6)))) {
                        return true;
                    }
                }
                catch (ParseException ex) {
                    ex.printStackTrace();
                    final Date parse = null;
                    continue;
                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
        return false;
    }

    public static boolean validateIdCard18(final String s) {
        final int length = s.length();
        boolean b = false;
        if (length == 18) {
            final String substring = s.substring(0, 17);
            final String substring2 = s.substring(17, 18);
            final boolean num = isNum(substring);
            b = false;
            if (num) {
                final char[] charArray = substring.toCharArray();
                b = false;
                if (charArray != null) {
                    final String checkCode18 = getCheckCode18(getPowerSum(converCharToInt(charArray)));
                    final int length2 = checkCode18.length();
                    b = false;
                    if (length2 > 0) {
                        final boolean equalsIgnoreCase = checkCode18.equalsIgnoreCase(substring2);
                        b = false;
                        if (equalsIgnoreCase) {
                            b = true;
                        }
                    }
                }
            }
        }
        return b;
    }

    public static boolean validateTWCard(final String s) {
        final String substring = s.substring(0, 1);
        final String substring2 = s.substring(1, 9);
        final String substring3 = s.substring(9, 10);
        final Integer n = IdcardUtils.twFirstCode.get(substring);
        Integer n2 = n / 10 + 9 * (n % 10);
        final char[] charArray = substring2.toCharArray();
        Integer n3 = 8;
        for (int length = charArray.length, i = 0; i < length; ++i) {
            n2 += Integer.valueOf(new StringBuilder(String.valueOf(charArray[i])).toString()) * n3;
            --n3;
        }
        int n4;
        if (n2 % 10 == 0) {
            n4 = 0;
        }
        else {
            n4 = 10 - n2 % 10;
        }
        final int intValue = Integer.valueOf(substring3);
        boolean b = false;
        if (n4 == intValue) {
            b = true;
        }
        return b;
    }
}

